home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / patch12.arc / PIPES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-02  |  1.5 KB  |  85 lines

  1. /* a simulation for the Unix popen() and pclose() calls on MS-DOS */
  2. /* only one pipe can be open at a time */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <io.h>
  8.  
  9. static char pipename[128], command[128];
  10. static int wrpipe;
  11.  
  12. static void Mktemp(char *);
  13.  
  14. FILE *popen(char *cmd, char *flags)
  15. {
  16.   wrpipe = (strchr(flags, 'w') != NULL);
  17.  
  18.   if ( wrpipe )
  19.   {
  20.     strcpy(command, cmd);
  21.     strcpy(pipename, "~WXXXXXX");
  22.     Mktemp(pipename);
  23.     return fopen(pipename, flags);  /* ordinary file */
  24.   }
  25.   else
  26.   {
  27.     strcpy(pipename, "~RXXXXXX");
  28.     Mktemp(pipename);
  29.     strcpy(command, cmd);
  30.     strcat(command, ">");
  31.     strcat(command, pipename);
  32.     system(command);
  33.     return fopen(pipename, flags);  /* ordinary file */
  34.   }
  35. }
  36.  
  37. int pclose(FILE *pipe)
  38. {
  39.   int rc;
  40.  
  41.   if ( fclose(pipe) == EOF )
  42.     return EOF;
  43.  
  44.   if ( wrpipe )
  45.   {
  46.     if ( command[strlen(command) - 1] == '!' )
  47.       command[strlen(command) - 1] = 0;
  48.     else
  49.       strcat(command, "<");
  50.  
  51.     strcat(command, pipename);
  52.     rc = system(command);
  53.     unlink(pipename);
  54.     return rc;
  55.   }
  56.   else
  57.   {
  58.     unlink(pipename);
  59.     return 0;
  60.   }
  61. }
  62.  
  63. static
  64. void Mktemp(char *file)
  65. {
  66.   char fname[32], *tmp, *bsp;
  67.  
  68.   tmp = getenv("TMP");
  69.  
  70.   if ( tmp != NULL )
  71.   {
  72.     strcpy(fname, file);
  73.     bsp = strcpy(file, tmp);
  74.     while ((bsp=strchr(bsp, '/')) != NULL)
  75.         *bsp = '\\';
  76.  
  77.     if ( file[strlen(file) - 1] != '\\' )
  78.       strcat(file, "\\");
  79.  
  80.     strcat(file, fname);
  81.   }
  82.  
  83.   mktemp(file);
  84. }
  85.